package org.dromara.resource.controller.api;

import org.dromara.common.core.domain.R;
import org.dromara.common.sse.dto.SseMessageDto;
import org.dromara.common.sse.utils.SseMessageUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: zhou shuai
 * @date: 2024/9/26 20:31
 * @version: v1
 */
@RestController
@RequestMapping("/api/resource/message")
public class ResourceMessageApiController {

    /**
     * 发送消息
     *
     * @param sessionKeyList session主键 一般为用户id
     * @param message    消息文本
     */
    @PostMapping("/publish")
    public R<Void> publishMessage(@RequestParam("sessionKeyList") List<Long> sessionKeyList, @RequestParam("message") String message) {
        SseMessageDto dto = new SseMessageDto();
        dto.setMessage(message);
        dto.setUserIds(sessionKeyList);
        SseMessageUtils.publishMessage(dto);
        return R.ok();
    }

    /**
     * 发布订阅的消息(群发)
     *
     * @param message 消息内容
     */
    @PostMapping("/publish/all")
    public R<Void> publishAll(@RequestParam("message") String message) {
        SseMessageUtils.publishAll(message);
        return R.ok();
    }

}
